console.log('凤凰网新闻脚本注入');

  // ============> utils
// 提取文章类型新闻页的信息

// function extractNewDetail(){
//   let root = document.querySelector('#root');
//   let newDivs = Array.from(root.querySelectorAll('div'));
//   if (Array.isArray(newDivs) && newDivs.length > 0){
//     let classNameReg = /^index_main_content_.*/ig;
//     let newContainerReg = /^index_articleBox_.*/ig;
//     let newTextContentReg = /^index_text_.*/ig;
//     let newContainer = newDivs.find(dom => classNameReg.test(dom.className));
//     let newDetailDom = Array.from(newContainer.querySelectorAll('div')).find(dom => newContainerReg.test(dom.className));
//     let newDetailContent = Array.from(newDetailDom.children).find(dom => newTextContentReg.test(dom.className));
//     let contents = false;
//     if (newDetailContent){
//       const texts = Array.from(newDetailContent.getElementsByTagName('p'));
//       const newTexts = texts.reduce((txts, currentDom) => {
//         if (currentDom.className.includes('picIntro') || currentDom.className.includes('picInfo')){
//           return txts;
//         } else {
//           return txts += currentDom.textContent + '\n';
//         }
//       }, '');
//       console.log('新闻文本内容如下：');
//       console.log(newTexts);
//       contents = {
//         text: newTexts,
//         image: [],
//         video: []
//       };
//       const imgs = Array.from(newDetailDom.querySelectorAll('img'));
//       if (imgs.length > 0){
//         imgs.forEach(item => {
//           if (item.dataset.lazyload){
//             contents.image.push(item.dataset.lazyload);
//           } else if (item.src){
//             contents.image.push(item.src);
//           }
//         })
//         console.log('新闻图片内容如下：');
//         console.log(contents.image);
//       }
//     }
//     return contents;
//   } else {
//     return false;
//   }
// }

// 文章类新闻信息提取 *.ifeng.com/c/
function extractNewDetail(){
  let filterReg = {
    indexMainContent: /^index_main_content_.*/ig,
    indexArticleBox: /^index_articleBox_.*/ig,
    indexText: /^index_text_.*/ig,
    indexVideo: /^index_video_.*/ig
  };
  let root = document.querySelector('#root');
  let allDivs = Array.from(root.querySelectorAll('div'));
  let indexMainContent = allDivs.find(div => filterReg.indexMainContent.test(div.className));
  let articleBox = Array.from(indexMainContent.querySelectorAll('div')).find(div => filterReg.indexArticleBox.test(div.className));
  let indexTexts = Array.from(articleBox.children).filter(child => {
    return filterReg.indexText.test(child.className);
  });
  let indexVideos = Array.from(articleBox.children).filter(child => filterReg.indexVideo.test(child.className));
  let contents = {
    text: '',
    image: [],
    video: []
  };

  if (indexTexts.length > 0){
    indexTexts.forEach(indexText => {
      dealIndexText(indexText, contents);
    });
    
  }
  if (indexVideos.length > 0) {
    indexVideos.forEach(indexVideo => {
      dealIndexVideo(indexVideo, contents);
    });
  }
  if (!contents.text && contents.image.length < 1 && contents.video.length < 1) {
    return false;
  } else {
    console.log('新闻的文本信息如下:');
    console.log(contents.text);
    console.log('新闻的图片信息如下：');
    console.log(contents.image);
    console.log('新闻的视频信息如下');
    console.log(contents.video);
    return contents;
  }
}
// 视频类新闻信息提取v.ifeng.com/c/
function extractVideoDetail(){
  let filterReg = {
    indexContent: /^index_content_.*/ig,
    indexPlayerContainer: /^index_playerContainer_.*/ig
  };
  let root = document.querySelector('#root');
  let allDivs = Array.from(root.querySelectorAll('div'));
  let indexContent = allDivs.find(div => filterReg.indexContent.test(div.className));
  let playerContainer = Array.from(indexContent.querySelectorAll('div')).find(div => filterReg.indexPlayerContainer.test(div.className));
  let videos = Array.from(playerContainer.querySelectorAll('video'));
  let contents = {
    text: '',
    image: [],
    video: []
  };
  if (Array.isArray(videos) && videos.length > 0){
    videos.forEach(video => {
      if (video.src){
        contents.video.push(video.src);
      }
    });
    console.log('获取的新闻信息如下： ')
    console.log(contents)
    return contents;
  } else {
    console.log('提取视频获取dom有问题')
    return false;
  }

}
// 处理找出来的.index_text_...中的文字和图片
function dealIndexText(indexText, contents){
  const allP = Array.from(indexText.getElementsByTagName('p'));
  contents.text = allP.reduce((txts, currentDom) => {
    if (currentDom.className.includes('picIntro') || currentDom.className.includes('picInfo')){
      return txts;
    } else {
      return txts += currentDom.textContent + '\n';
    }
  }, contents.text);
  Array.from(indexText.querySelectorAll('img')).forEach(img => {
    if (img.dataset.lazyload){
      contents.image.push(img.dataset.lazyload);
    } else if (img.src){
      contents.image.push(img.src);
    }
  });
}
function dealIndexVideo(indexVideo, contents){
  Array.from(indexVideo.getElementsByTagName('video')).forEach(video => {
    if (video.src){
      contents.video.push(video.src);
    }
  });
  
}
// 处理新闻请求
function handleNew(){
  let videoNewReg = /^v\.ifeng\.com/i;
  let subjectReg = /^\/c\//i;
  if (videoNewReg.test(window.location.hostname)){
    return extractVideoDetail();
  } else if (subjectReg.test(window.location.pathname)){
    return extractNewDetail();
  } else {
    return false;
  }
}
// 消息监听
chrome.runtime.onMessage.addListener((message, sender, sendMessage) => {
  // 后台发送的采集新闻页信息消息
  if (message.id === 'newDetail'){
    const result = handleNew();
    sendMessage(result);
  }
});